package user1;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/5/11 9:00
 * @description:
 */
public class User1IO implements User1Dao {

  @Override
  public void register(user user1) {
    BufferedWriter bw=null;

    try {
      bw=new BufferedWriter(new FileWriter("d:\\user.txt"));

      bw.write(user1.getId()+"--"+user1.getPassword()+"--"+user1.getAge());
      bw.newLine();
      bw.flush();
    } catch (IOException e) {
      e.printStackTrace();
      System.out.println(""+e.getMessage());
    }finally {
      if(bw!=null){
        try {
          bw.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }


  }

  @Override
  public boolean login(String name, String Password) {

    BufferedReader br=null;

    try {
      br=new BufferedReader(new FileReader("d:\\user.txt"));
      String line=null;
      while ((line=br.readLine())!=null){
        String [] data=line.split("--");
        if(data[1].equals(name)&&data[2].equals(Password)){
          return true;
        }
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
    return false;
  }

  @Override
  public void quereyAll() {
   BufferedReader br=null;
    try {
      br=new BufferedReader(new FileReader("d:\\user.txt"));
      String line=null;
      while ((line=br.readLine())!=null){
        System.out.println(line);
      }
    } catch (IOException e) {
      e.printStackTrace();
    }finally {
      if(br !=null){
        try {
          br.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }

  }
}
